package com.example.weather;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.lidroid.xutils.BitmapUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by asd on 2016/9/18.
 */
public class GongGao extends Activity {
    private TextView tvTitle;
    private TextView tvData1;
    private ImageView ivData1;
    private TextView tvData2;
    private ImageView ivData2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gonggao);
        init();
        getData();

    }

    private void init() {
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvData1 = (TextView) findViewById(R.id.tv_data1);
        ivData1 = (ImageView) findViewById(R.id.iv_data1);
        tvData2 = (TextView) findViewById(R.id.tv_data2);
        ivData2 = (ImageView) findViewById(R.id.iv_data2);
    }

    //通过自己搭的服务器返回的json数据实现界面加载
    public void getData() {
        RequestQueue requestQueue = Volley.newRequestQueue(GongGao.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://192.168.191.1:8080/weather/weatherService", new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                BitmapUtils bitmap = new BitmapUtils(GongGao.this);
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    JSONArray jsonArray = jsonObject.getJSONArray("jdsonarray");
                    JSONObject jsonData = (JSONObject) jsonArray.get(0);
                    String sdata = jsonData.getString("data");
                    tvTitle.setText(jsonObject.getString("txt"));
                    tvData1.setText(sdata);
                    String url = jsonData.getString("array");
                    JSONObject jsonData1 = (JSONObject) jsonArray.get(1);
                    String sdata1 = jsonData1.getString("data");
                    tvData2.setText(sdata1);
                    String url1 = jsonData1.getString("array");
                    bitmap.display(ivData2, url1);
                    bitmap.display(ivData1, url);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.v("msg", "连接失败");
            }
        });
        requestQueue.add(stringRequest);
        requestQueue.start();
    }
}

